package com.udaos.modules.company.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.annotation.SLog;
import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.company.Ud_company_region;
import com.udaos.modules.company.services.UdCompanyRegionService;


/**
 * 主体-片区WEB层
 * @author mutaotao
 * @time 2016-11-07 17:19:18
 * 
 */
@IocBean
@At("/platform/companyRegion")
@Filters({ @By(type = PrivateFilter.class)})
public class UdCompanyRegionController {
	
	@Inject
	private UdCompanyRegionService udCompanyRegionService;


	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/companyregion/index.html")
	@RequiresAuthentication
	public void index() {

	}
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		return udCompanyRegionService.init(length, start, draw);
	}
	
	/**
	 * 新增
	 * @param unitid
	 * @return
	 */
	@At
	@Ok("beetl:/platform/base/companyregion/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("companys", udCompanyRegionService.findCompanys());
		map.put("regions", udCompanyRegionService.findRegions());
		return map;
	}
	
	/**
	 * 新增
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object addDo(@Param("..") Ud_company_region company_region, HttpServletRequest req) {
		try {
            udCompanyRegionService.save(company_region);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_company_region company_region = udCompanyRegionService.fetch(id);
        	company_region.setDelFlag(true);
        	udCompanyRegionService.updateIgnoreNull(company_region);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}